package com.clickpaas.workinghours.dao.Impl;

import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.  d20210220155135.codegen.*;
import com.bizcloud.ipaas.tbba4bf83484b4ed2aacba581fbfeac4d.  d20210220155135.model.*;
import com.clickpaas.workinghours.bean.WorkingHoursDTOBean;
import com.clickpaas.workinghours.dao.TimeCardDateDao;
import com.clickpaas.workinghours.entity.TimeCardDateEntity;
import com.clickpaas.workinghours.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Repository
public class TimeCardDateDaoImpl implements TimeCardDateDao {

    @Resource
    private WorkingHoursDTOBean dtoBean;
    @Resource
    private TimecarddateApi timecarddateApi;
    /**
     * 根据条件查询
     *
     * @param timeCardDateDTO
     */
    private List<TimeCardDateDTOResponse> getDataByCondtion(TimeCardDateDTO timeCardDateDTO) {
        DataResponseListTimeCardDateDTO dataInfo = timecarddateApi.findTimeCardDateUsingPOST(timeCardDateDTO);
        if(null == dataInfo
                || null == dataInfo.getData()
                || dataInfo.getData().size()<=0
                || !"SUCCESS".equals(dataInfo.getMessage())){
            return null;
        }
        return dataInfo.getData();
    }

    /**
     *
     * @param entity
     */
    @Override
    public List<TimeCardDateDTOResponse> queryDataInfo(TimeCardDateEntity entity) {
        TimeCardDateDTO timeCardDateDTO = dtoBean.timeCardDateDTO();
        try {
            SpringUtil.copyPropertiesIgnoreNull(entity,timeCardDateDTO);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",entity);
        }
        return getDataByCondtion(timeCardDateDTO);
    }

    /**
     * 工时日期数据入库
     *
     * @param entity
     */
    @Override
    public TimeCardDateSaveOrUpdateDataResponseObject insertDataInfo(TimeCardDateEntity entity) {
        TimeCardDateDTOUpdate dtoUpdate = dtoBean.timeCardDateDTOUpdate();
        try {
            SpringUtil.copyPropertiesIgnoreNull(entity,dtoUpdate);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象是：【{}】",entity);
        }
        SaveOrUpdateTimeCardDateDTO saveInfo = new SaveOrUpdateTimeCardDateDTO();
        saveInfo.setQuery(dtoBean.timeCardDateDTOQuery());
        saveInfo.setUpdate(dtoUpdate);
        return timecarddateApi.saveOrUpdateTimeCardDateUsingPOST(saveInfo);
    }

    /**
     * 工时日期数据更新
     *
     * @param query
     * @param update
     */
    @Override
    public TimeCardDateSaveOrUpdateDataResponseObject updateDataInfo(TimeCardDateEntity query, TimeCardDateEntity update) {
        TimeCardDateDTOQuery queryData = dtoBean.timeCardDateDTOQuery();
        TimeCardDateDTOUpdate updateData = dtoBean.timeCardDateDTOUpdate();
        try {
            BeanUtils.copyProperties(query,queryData);
            BeanUtils.copyProperties(update,updateData);
        }catch (Exception e){
            log.info("对象转换失败！转换的对象query:【{}】,update:【{}】",query,update);
        }
        SaveOrUpdateTimeCardDateDTO saveInfo = new SaveOrUpdateTimeCardDateDTO();
        saveInfo.setQuery(queryData);
        saveInfo.setUpdate(updateData);
        return timecarddateApi.saveOrUpdateTimeCardDateUsingPOST(saveInfo);
    }

}
